package lee.vioson.remotedata.repositories

import lee.vioson.remotedata.bean.BaseRsp
import lee.vioson.remotedata.throwable.NetException
import lee.vioson.remotedata.throwable.TokenError


/**
 *Author:viosonlee
 *Date:2021/6/18
 *DESCRIPTION:
 */
abstract class BaseRepository {
    val pageSize = 20

    @Throws(NetException::class)
    fun <T> handleResponse(baseRsp: BaseRsp<T>): BaseRsp<T> {
        when {
            baseRsp.success -> {
                return baseRsp
            }

            baseRsp.isLoginExpired -> {
                throw TokenError(baseRsp.msg)
            }

            else -> {
                throw NetException(baseRsp.code.toString(), baseRsp.msg)
            }
        }
    }

    @Throws(NetException::class)
    fun <T> handleNotEmptyResponse(baseRsp: BaseRsp<T>): T {
        when {
            baseRsp.isSuccessAndNotEmpty -> {
                return baseRsp.data!!
            }

            baseRsp.isLoginExpired -> {
                throw TokenError(baseRsp.msg)
            }

            else -> {
                throw NetException(baseRsp.code.toString(), baseRsp.msg)
            }
        }
    }

}